const vm = Vue.createApp({
    data() {
        return {
            OrderData: [], //订单
            seek: '',
            state: 1, //1全部，2未完成，3待收货，4已完成，5已取消
            Tracking: '',
        };
    },
    mounted() {
        this.GetOrderList();
    },
    methods: {
        GetQuery(id) {
            var index = layer.msg('订单查询中...', {
                icon: 16,
                time: 9999999
            });
            $.ajax({
                type: "post",
                url: "/main.php?act=OrderList&type=QueryDetails",
                data: {
                    id: id
                },
                dataType: "json",
                success: function (data) {
                    layer.close(index);
                    if (data.code == 1) {
                        data = data.data;
                        var content = '';
                        if (data.ApiType != undefined) {
                            content += '<p style="text-align: center;font-size: 1.3em;color: seagreen;margin-bottom: 0;">订单实时状态</p><table class="layui-table layui-text" lay-size="sm" lay-skin="row"><colgroup>\n' +
                                '    <col width="30%">\n' +
                                '    <col width="50%">' +
                                '    <col width="20%">\n' +
                                '    <col>\n' +
                                '  </colgroup><thead>\n' +
                                '    <tr>\n' +
                                '      <th>类型</th>\n' +
                                '      <th>实时状态</th>' +
                                '      <th>操作</th>\n' +
                                '    </tr> \n' +
                                '  </thead><tbody>';
                            content += '<tr>\n' +
                                '      <td>订单状态</td>\n' +
                                '      <td>' + data.ApiState + '</td>' +
                                '      <td>无</td>\n' +
                                '    </tr>';
                            content += '<tr>\n' +
                                '      <td>初始数量</td>\n' +
                                '      <td>' + data.ApiInitial + '</td>' +
                                '      <td>无</td>\n' +
                                '    </tr>';
                            content += '<tr>\n' +
                                '      <td>下单数量</td>\n' +
                                '      <td>' + data.ApiNum + '</td>' +
                                '      <td>无</td>\n' +
                                '    </tr>';
                            content += '<tr>\n' +
                                '      <td>当前数量</td>\n' +
                                '      <td>' + data.ApiPresent + '</td>' +
                                '      <td>无</td>\n' +
                                '    </tr>';
                            content += '<tr>\n' +
                                '      <td>提交时间</td>\n' +
                                '      <td>' + data.ApiTime + '</td>' +
                                '      <td>无</td>\n' +
                                '    </tr>';
                            if (data.ApiError != '无') {
                                content += '<tr>\n' +
                                    '      <td>备注信息</td>\n' +
                                    '      <td>' + data.ApiError + '</td>' +
                                    '      <td>无</td>\n' +
                                    '    </tr>';
                            }
                            content += '</tbody></table><hr>';
                        }
                        content += '<table class="layui-table layui-text" lay-size="sm" lay-skin="row"><colgroup>\n' +
                            '    <col width="30%">\n' +
                            '    <col width="50%">' +
                            '    <col width="20%">\n' +
                            '    <col>\n' +
                            '  </colgroup><thead>\n' +
                            '    <tr>\n' +
                            '      <th>类型</th>\n' +
                            '      <th>状态</th>' +
                            '      <th>操作</th>\n' +
                            '    </tr> \n' +
                            '  </thead><tbody>';

                        content += '<tr>\n' +
                            '      <td>订单编号</td>\n' +
                            '      <td>' + data.order + '</td>' +
                            '      <td>无</td>\n' +
                            '    </tr>';

                        content += '<tr>\n' +
                            '      <td>商品名称</td>\n' +
                            '      <td>' + data.name + '</td>' +
                            '      <td>无</td>\n' +
                            '    </tr>';
                        content += '<tr>\n' +
                            '      <td>订单状态</td>\n' +
                            '      <td>' + data.state + '</td>' +
                            '      <td>' + (data.state == '已评价' ? '<a href="javascript:merchandise.mark(' + data.gid + ')">查看</a>' : '无') + '</td>\n' +
                            '    </tr>';
                        content += '<tr>\n' +
                            '      <td>购买数量</td>\n' +
                            '      <td>' + data.num + '</td>' +
                            '      <td>无</td>\n' +
                            '    </tr>';
                        content += '<tr>\n' +
                            '      <td>付款方式</td>\n' +
                            '      <td>' + data.type + '</td>' +
                            '      <td>无</td>\n' +
                            '    </tr>';
                        content += '<tr>\n' +
                            '      <td>支付金额</td>\n' +
                            '      <td>' + (data.price - 0).toFixed(2) + (data.type == '积分兑换' ? '积分' : '元') + '</td>' +
                            '      <td>无</td>\n' +
                            '    </tr>';
                        if (data.coupon != -1) {
                            content += '<tr>\n' +
                                '      <td style="color:red">优惠金额</td>\n' +
                                '      <td style="color:red">' + data.discounts + '元</td>' +
                                '      <td>无</td>\n' +
                                '    </tr>';
                            content += '<tr>\n' +
                                '      <td style="color:red">订单原价</td>\n' +
                                '      <td style="color:red">' + data.money + '元</td>' +
                                '      <td>无</td>\n' +
                                '    </tr>';
                            content += '<tr>\n' +
                                '      <td style="color:red">优惠券ID</td>\n' +
                                '      <td style="color:red">' + data.coupon + '号</td>' +
                                '      <td>无</td>\n' +
                                '    </tr>';
                        }

                        input = data.input;
                        if (input.constructor == Array) input = input.join('<br>');

                        content += '<tr>\n' +
                            '      <td>下单信息</td>\n' +
                            '      <td>' + input + '</td>' +
                            '      <td>无</td>\n' +
                            '    </tr>';

                        content += '<tr>\n' +
                            '      <td>创建时间</td>\n' +
                            '      <td>' + data.addtiem + '</td>' +
                            '      <td>无</td>\n' +
                            '    </tr>';

                        if (data.remark != undefined) {
                            content += '<tr>\n' +
                                '      <td>订单备注</td>\n' +
                                '      <td>' + data.remark + '</td>' +
                                '      <td>无</td>\n' +
                                '    </tr>';
                        }

                        if (data.Explain != -1) {
                            content += '<tr>\n' +
                                '      <td style="color:red">使用帮助</td>\n' +
                                '      <td>' + data.Explain + '</td>' +
                                '      <td>无</td>\n' +
                                '    </tr>';
                        }

                        if (data.token_arr != -1) {
                            var token_list = '';
                            $.each(data.token_arr, function (key, val) {
                                token_list += '卡' + (key + 1) + '：' + val + '<br>';
                            });
                            content += '<tr>\n' +
                                '      <td style="color: red">订单卡密</td>\n' +
                                '      <td>' + token_list + '</td>' +
                                '      <td>无</td>\n' +
                                '    </tr>';
                        }

                        content += '</tbody></table><style>#OrderQuery{padding: 0.5em;max-height: 86vh;overflow:hidden;overflow-y: auto}</style><hr>' + data.docs;
                        mdui.alert(content, '订单[' + data.order + ']详情！');
                    } else {
                        layer.msg(data.msg);
                    }
                },
                error: function () {
                    layer.alert('查询失败！');
                    layer.close(index);
                }
            });
        },
        GetOrderList(page = 1) {
            types = 1;
            query = 1;
            if (page == -1) {
                page = 1;
                this.OrderData = [];
                var index = layer.load(1);
            } else if (page == -2) {
                page = 1;
                this.OrderData = [];
                var index = layer.load(1);
                query = 2;
            } else if (page == -3) {
                page = 1;
                this.OrderData = [];
                var index = layer.load(1);
                types = 2;
            } else var index;

            let _this = this;
            if (types == 1) {
                $.ajax({
                    type: 'post',
                    url: '/main.php?act=OrderList&type=OrderAll',
                    data: {
                        seek: _this.seek,
                        state: _this.state,
                        page: page
                    },
                    dataType: 'json',
                    success: function (data) {
                        layer.close(index);
                        if (data.code == 1) {
                            _this.Tracking = data.Tracking;
                            if (data.data.length != 0) {
                                _this.OrderData.push.apply(_this.OrderData, data.data);
                                if (data.data.length == 6) _this.GetOrderList(page + 1);
                            } else if (query == 2) {
                                layer.alert('没有查询到任何订单<br>可能此订单是游客订单，未绑定用户,请使用游客查单模式！', {
                                    icon: 2
                                });
                            }
                        } else {
                            layer.msg(data.msg, {
                                icon: 2
                            });
                        }
                    },
                    error: function () {
                        layer.alert('加载失败！');
                    }
                });
            } else {
                $.ajax({
                    type: 'post',
                    url: '/main.php?act=OrderList&type=QueryList',
                    data: {
                        msg: _this.seek,
                    },
                    dataType: 'json',
                    success: function (data) {
                        layer.close(index);
                        if (data.code == 1) {
                            _this.Tracking = data.Tracking;
                            if (data.data.length != 0) {
                                _this.OrderData.push.apply(_this.OrderData, data.data);
                            } else layer.alert('没有查询到任何和[' + _this.seek + ']有关的订单<br>请输入下单时填写的第一行信息！<br>或者和搜索内容有关的订单并非游客订单!', {
                                icon: 2
                            });
                        } else {
                            layer.alert(data.msg, {
                                icon: 2
                            });
                        }
                    },
                    error: function () {
                        layer.alert('加载失败！');
                    }
                });
            }
        },
    }
}).mount('#Appid');